<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 店铺商品管理
 *
 * @icon fa fa-circle-o
 */
class Merchantgoods extends Backend
{

    /**
     * Merchant模型对象
     * @var \app\common\model\Merchant
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Merchantgoods();
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeDataList", $this->model->getTypeDataList());
    }
    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $filter = $this->request->request('filter');
        $op = json_decode($this->request->request("op"),true);
        $filterArr = (array)json_decode($filter, true);
        if($this->request->request("type_data") && $this->request->request("type_data")==3){
            $filterArr["type_data"]="1";
            $filterArr["card_type_id"]="0";
            $op["card_type_id"]=">";
            $op["type_data"]="=";
            $this->request->get(['filter' => json_encode($filterArr),'op'=>json_encode($op)]);
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->field("merchantgoods.*,card_type.name 'card_type.name'")
            ->join("card_type card_type","card_type.id=merchantgoods.card_type_id","left")
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit)->each(function ($v){
                if(!empty($v["start_time"])) $v["start_time_text"]=date("Y-m-d H:i:s",$v["start_time"]);
                if(!empty($v["end_time"])) $v["end_time_text"]=date("Y-m-d H:i:s",$v["end_time"]);
            });
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 查看详情
     * */
    public function see($id = null){
        $row = $this->model->alias("merchantgoods")
            ->field("merchantgoods.*,card_type.name 'card_type.name'")
            ->join("card_type card_type","card_type.id=merchantgoods.card_type_id","left")
            ->where(["merchantgoods.id"=>$id])
            ->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if(!empty($row["images"])) $row["images"]=explode(",",$row["images"]);
        $row["lifespan"]=0;
        if(!empty($row["start_time"])) $row["lifespan"]=date("Y-m-d H:i:s",$row["start_time"])."——".date("Y-m-d H:i:s",$row["end_time"]);
        $this->assign("row",$row);
        return $this->view->fetch();
    }
}
